package com.example.sunmiv1.activity;

import android.content.Context;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.sunmiv1.R;
import com.example.sunmiv1.adapter.ModifyRecordAdapter;
import com.example.sunmiv1.base.BaseActivity;
import com.example.sunmiv1.entity.ModifyRecordBean;
import com.example.sunmiv1.utils.Tools;
import com.example.sunmiv1.utils.net.Common;
import com.example.sunmiv1.utils.net.GsonUtils;
import com.example.sunmiv1.utils.net.HttpUtils;
import com.example.sunmiv1.utils.net.LoadingDialog;
import com.example.sunmiv1.utils.net.TextCallBack;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ModifyRecordActivity extends BaseActivity {


    @BindView(R.id.title_back)
    ImageView titleBack;
    @BindView(R.id.title_title)
    TextView titleTitle;
    @BindView(R.id.modifyRecord_listView)
    PullToRefreshListView modifyRecordListView;

    private int page = 1;
    private Context context;
    private LoadingDialog loadingDialog;

    private ModifyRecordBean modifyRecordBean;
    private ModifyRecordAdapter modifyRecordAdapter;
    private List<ModifyRecordBean.DataBean.ListBean> list = new ArrayList<>();

    @Override
    protected void initView() {
        setContentView(R.layout.activity_modify_record);
        ButterKnife.bind(this);
        context = this;
        loadingDialog = new LoadingDialog(context);
        titleTitle.setText("修改記錄");
    }

    @Override
    protected void initData() {
        getRecord();
    }

    private void setModifyRecordAdapter() {
        if (modifyRecordAdapter == null) {
            modifyRecordAdapter = new ModifyRecordAdapter(list);
            modifyRecordListView.setAdapter(modifyRecordAdapter);
        } else {
            modifyRecordAdapter.notifyDataSetChanged();
        }
    }

    @Override
    protected void initListener() {
        modifyRecordListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                page = 1;
                list.clear();
                setModifyRecordAdapter();
                getRecord();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                getRecord();
            }
        });
    }

    @OnClick(R.id.title_back)
    public void onViewClicked() {
        finish();
    }

    private void getRecord() {
        loadingDialog.show();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("id", getIntent().getStringExtra("id"));
            jsonObject.put("page", page);
            jsonObject.put("limit", 20);
            HttpUtils.post(context, Common.SERIAL_MODIFY_RECORD, Tools.createParameterCode(jsonObject), new TextCallBack() {
                @Override
                protected void onSuccess(String text) {
                    loadingDialog.dismiss();
                    modifyRecordListView.onRefreshComplete();
                    modifyRecordBean = GsonUtils.JsonToBean(text, ModifyRecordBean.class);
                    Toast.makeText(context, modifyRecordBean.getMessage(), Toast.LENGTH_SHORT).show();
                    if (modifyRecordBean.getStatus() == 1) {
                        page++;
                        list.addAll(modifyRecordBean.getData().getList());
                        setModifyRecordAdapter();
                    }
                }

                @Override
                protected void onFailure(ResponseException e) {
                    loadingDialog.dismiss();
                    modifyRecordListView.onRefreshComplete();
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

}
